package com.xxx.service.impl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxx.common.IdWorkerManage;
import com.xxx.common.dto.Page;
import com.xxx.common.dto.Pagination;
import com.xxx.dao.ArticleTypeDao;
import com.xxx.model.ArticleType;
import com.xxx.service.ArticleTypeService;

@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {
	
	@Autowired
	private ArticleTypeDao articleTypeDao;
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public long saveOrUpdate(ArticleType articleType) throws Exception {
		if(articleType.getId() == null){
			articleType.setId(IdWorkerManage.getId());
			return articleTypeDao.save(articleType);
		}
		return articleTypeDao.update(articleType);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean delete(long id) throws Exception{
		return articleTypeDao.delete(id);
	}

	@Override
	public ArticleType getEntity(long id) throws Exception{
		return articleTypeDao.getEntity(id);
	}

	@Override
	public int getCount(Map<String, Object> whereMap) {
		return articleTypeDao.getCount(whereMap);
	}
	
	@Override
	public List<ArticleType> getList(Map<String, Object> whereMap) {
		return articleTypeDao.getList(whereMap);
	}

	@Override
	public List<ArticleType> getList(Map<String, Object> whereMap, Pagination pagination) {
		return articleTypeDao.getList(whereMap, pagination);
	}
	
	@Override
	public Page<ArticleType> getPage(Map<String, Object> whereMap, Pagination pagination) {
		int count = this.getCount(whereMap);
		if(count > 0){
			List<ArticleType> list = this.getList(whereMap, pagination);
			return new Page<ArticleType>(pagination.getCurrentPage(), pagination.getPageSize(), count, list);
		}
		return new Page<ArticleType>(pagination.getCurrentPage(), count);
	}
}
